-- wezterm配置文件

local wezterm = require 'wezterm';

local dracula = require('dracula');   -- 导入dracula主题配置文件

local act = wezterm.action;

local config = {
    check_for_updates = false,                           -- 开关会定期检查 github 上是否有新的稳定版本可用
    -- check_for_updates_interval_seconds = 86400,       -- 检查更新时间设置，默认情况下每 24 小时检查一次
    adjust_window_size_when_changing_font_size = false,  -- 调整窗口时改变字体大小
    -- enable_tab_bar = false,           -- 标签状态开关
    window_background_opacity = 0.7,     -- 窗口背景透明度
    text_background_opacity = 0.95,      -- 字体背景透明度
    audible_bell = "Disabled",           -- 铃声禁止
    -- inactive_pane_hsb = {
    --     hue = 1.0,
    --     saturation = 1.0,
    --     brightness = 1.0,
    -- },
    -- default_prog = { '/bin/bash', '-l' },           -- 在登录 bash 模式
    -- default_prog = { '/usr/local/bin/fish', '-l' }, -- Spawn a fish shell in login mode 在登录模式下生成一个fish

    -- 字体配置
    -- font = wezterm.font(
    --     'JetBrainsMono NF', { 
    --         weight = 'Bold', italic = true 
    --     }
    -- ),
    -- font = wezterm.font_with_fallback({
    --     'JetBrainsMono NF',
    --     -- "Dank Mono",
    --     -- "Hack Nerd Font Mono",
    --     -- "PowerlineExtraSymbols",
    --     -- "Noto Color Emoji"
    -- }),
    font_size = 12,               -- 字体大小
    -- font_dirs = { 'fonts' },
    
    -- font = wezterm.font 'Fira Code',
    -- You can specify some parameters to influence the font selection;
    -- for example, this selects a Bold, Italic font variant.
    -- font = wezterm.font('JetBrains Mono', { weight = 'Bold', italic = true }),
    -- font = wezterm.font_with_fallback({"CaskaydiaCove Nerd Font", "Cascadia Code", "Fira Code"}),

    -- freetype_load_target = "Light",
    -- font = wezterm.font_with_fallback({
    --     {
    --       family = "Cartograph CF",
    --       weight = "Regular"
    --     },
    --     {
    --       -- Fallback font with all the Nerdfont Symbols
    --       family = "FiraCode Nerd Font Mono",
    --       scale = 0.9
    --     },
    -- }),
    -- 指定当前工作目录
    -- default_cwd = "~/Document/XWorkSpace",

    -- 主题
    colors = dracula,
    -- color_scheme = "Brogrammer",          -- 主题
    -- set default theme to dracula official conf
    -- tab_bar_at_bottom = true, -- 标签栏设置为底部
    -- use_fancy_tab_bar = false,
    disable_default_key_bindings = true,  -- 禁止默认按键绑定

    -- macOS 左右选项键 配置
    send_composed_key_when_left_alt_is_pressed = false,
    send_composed_key_when_right_alt_is_pressed = true,

    -- 按住Shift+鼠标左键选择区域
    mouse_bindings = {
        {
            event = { Up = { streak = 1, button = 'Left' } },
            mods = 'SHIFT',
            action = wezterm.action.ExtendSelectionToMouseCursor 'Word',
        },
    },
    
    use_dead_keys = false, -- 禁用死键处理，以便^可以通过单次按键使用
    -- launch_menu = {},
    leader = { key="a", mods="CTRL" },   -- 设置修饰键/前导键为：Ctrl + a
    -- leader = { key='a', mods='CTRL', timeout_milliseconds = 1000 },      -- 设置修饰键/前导键为：Ctrl + a
    -- leader = { key='VoidSymbol', mods = '', timeout_milliseconds=1000 }, -- 设置修饰键/前导键为：VoidSymbol  使用VoidSymbol并CapsLock用作 不影响班次/大写状态
    keys = { -- 快捷键
        -- Turn off the default CMD-m Hide action, allowing CMD-m to 关闭默认的 CMD-m 隐藏动作，允许 CMD-m
        -- { key = 'm',mods='CMD', action = 'DisableDefaultAssignment' },
        -- Send "CTRL-A" to the terminal when pressing CTRL-A, CTRL-A 按下 CTRL-A、CTRL-A 时向终端发送“CTRL-A”
        -- { key='a', mods='LEADER|CTRL', action=wezterm.action.SendString '\x01',},
        -- 复制 黏贴
        -- { key="v", mods="CTRL|SHIFT", action=wezterm.action{PasteFrom="Clipboard"},},
        -- { key="c", mods="CTRL|SHIFT", action="Copy"},
        -- { key="v", mods="CTRL", action=wezterm.action{PasteFrom="Clipboard"},},
        -- { key="c", mods="CTRL", action="Copy"},
        { key="v",   mods="SHIFT|CTRL",     action=wezterm.action.PasteFrom 'Clipboard'},
        { key="c",   mods="SHIFT|CTRL",     action=wezterm.action.CopyTo 'Clipboard'},

        -- 字体大小快捷键(放大/+、缩小/-、复原0) 
        { key="=", mods="CTRL", action="IncreaseFontSize"},
        { key="-", mods="CTRL", action="DecreaseFontSize"},
        { key="0", mods="CTRL", action="ResetFontSize"},
        -- 隐藏
        -- { key="m", mods="ALT", action="Hide"},
        -- 打开启动列表（命令列表）
        { key ='/', mods='ALT', action = wezterm.action.ShowLauncher },

        -- 窗格创建
        -- - 垂直切分创建窗格
        -- { key = 'Enter', mods='ALT', action=wezterm.action.SplitHorizontal{domain='CurrentPaneDomain'},},
        { key = 'Enter', mods='ALT',    action=act.SplitHorizontal{domain='CurrentPaneDomain'},},
        -- - 水平切分创建窗格
        -- { key='Enter', mods='ALT|SHIFT',action=wezterm.action.SplitVertical{domain='CurrentPaneDomain'},},
        { key='Enter', mods='ALT|SHIFT',   action=act.SplitVertical{domain='CurrentPaneDomain'},},
        -- 窗口切换
        -- - Alt+方向键
        { key='LeftArrow', mods='ALT',action=act.ActivatePaneDirection 'Left',},
        { key='RightArrow',mods='ALT',action=act.ActivatePaneDirection 'Right',},
        { key='UpArrow',   mods='ALT',action=act.ActivatePaneDirection 'Up',},
        { key='DownArrow', mods='ALT',action=act.ActivatePaneDirection 'Down',},
        -- - Alt+h/j/k/l
        { key='h',         mods='ALT',action=act.ActivatePaneDirection 'Left',},
        { key='l',         mods='ALT',action=act.ActivatePaneDirection 'Right',},
        { key='k',         mods='ALT',action=act.ActivatePaneDirection 'Up',},
        { key='j',         mods='ALT',action=act.ActivatePaneDirection 'Down',},
        -- { key = "c", mods = "LEADER",       action=wezterm.action{SpawnTab="CurrentPaneDomain"}},
        -- { key = "h", mods = "LEADER",       action=wezterm.action{ActivatePaneDirection="Left"}},
        -- { key = "j", mods = "LEADER",       action=wezterm.action{ActivatePaneDirection="Down"}},
        -- { key = "k", mods = "LEADER",       action=wezterm.action{ActivatePaneDirection="Up"}},
        -- { key = "l", mods = "LEADER",       action=wezterm.action{ActivatePaneDirection="Right"}},
        -- 使用默认字母的窗格选择模式
        { key='w',         mods='ALT',action=act.PaneSelect },
        -- 窗口尺寸
        { key = "H", mods = "LEADER|SHIFT", action=wezterm.action{AdjustPaneSize={"Left", 5}}},
        { key = "J", mods = "LEADER|SHIFT", action=wezterm.action{AdjustPaneSize={"Down", 5}}},
        { key = "K", mods = "LEADER|SHIFT", action=wezterm.action{AdjustPaneSize={"Up", 5}}},
        { key = "L", mods = "LEADER|SHIFT", action=wezterm.action{AdjustPaneSize={"Right", 5}}},
        -- 使用数字的窗格选择模式
        { key='w',         mods='ALT|SHIFT',action=act.PaneSelect{alphabet='1234567890',}, },
        -- 使用窗格选择模式，但让它交换活动和选定的窗格
        -- { key='w',         mods='ALT|CTRL',action=act.PaneSelect{mode='SwapWithActive',}, },
        -- 缩放当前窗格
        { key='z',         mods='ALT',action=act.TogglePaneZoomState,},
        -- 分离窗格
        -- 关闭当前窗格
        -- { key="c", mods="ALT", action=wezterm.action.CloseCurrentPane{confirm=true},},
        { key="c", mods="ALT", action=act.CloseCurrentPane{confirm=true},},
        { key="q", mods="ALT", action=act.CloseCurrentPane{confirm=true},},
        { key="e", mods="ALT", action=act.CloseCurrentPane{confirm=true},},
        -- { key="&", mods="LEADER|SHIFT", action=wezterm.action{CloseCurrentTab={confirm=true}}},
        -- { key="x", mods="LEADER",       action=wezterm.action{CloseCurrentPane={confirm=true}}},
   
        -- 新建标签页
        -- { key='n', mods='ALT', action=act.SpawnTab 'CurrentPaneDomain',},
        { key='t', mods='ALT', action=act.SpawnTab 'CurrentPaneDomain',},
        -- 切换标签页
        { key='n', mods='ALT', action=act.ActivateTabRelative(-1),},
        { key='p', mods='ALT', action=act.ActivateTabRelative(1),},
        -- { key='[', mods='ALT', action=act.ActivateTabRelative(-1),},
        -- { key=']', mods='ALT', action=act.ActivateTabRelative(1),},
        -- { key = "1", mods = "LEADER",       action=wezterm.action{ActivateTab=0}},
        -- { key = "2", mods = "LEADER",       action=wezterm.action{ActivateTab=1}},
        -- { key = "3", mods = "LEADER",       action=wezterm.action{ActivateTab=2}},
        -- { key = "4", mods = "LEADER",       action=wezterm.action{ActivateTab=3}},
        -- { key = "5", mods = "LEADER",       action=wezterm.action{ActivateTab=4}},
        -- { key = "6", mods = "LEADER",       action=wezterm.action{ActivateTab=5}},
        -- { key = "7", mods = "LEADER",       action=wezterm.action{ActivateTab=6}},
        -- { key = "8", mods = "LEADER",       action=wezterm.action{ActivateTab=7}},
        -- { key = "9", mods = "LEADER",       action=wezterm.action{ActivateTab=8}},
        -- 移动标签页
        -- 关闭当前标签窗口
        -- { key='c', mods="ALT|SHIFT", action = wezterm.action.CloseCurrentTab{confirm=true},},
        { key='c', mods="ALT|SHIFT", action = act.CloseCurrentTab{confirm=true},},
        
        -- 窗口全屏
        { key='f', mods='ALT',       action=act.ToggleFullScreen,},
        { key='f', mods='ALT|SHIFT', action=act.ToggleFullScreen,},
        -- { key = "n", mods="SHIFT|CTRL",     action="ToggleFullScreen" },

        -- 快速选择
        -- { key='v', mods='ALT', action = wezterm.action.QuickSelect },
        { key='v', mods='ALT', action = act.QuickSelect },
        -- 快速选择url
        -- { key = 'u',mods='ALT',action=wezterm.action.QuickSelectArgs {patterns = {'https?://\\S+',},},},
        { key = 'u',mods='ALT',action=act.QuickSelectArgs {patterns = {'https?://\\S+',},},},
        -- 快速选择url并打开
        {
          key = 'u',
          mods = 'ALT|SHIFT',
          action = wezterm.action.QuickSelectArgs {
            label = 'open url',
            patterns = {
              'https?://\\S+',
            },
            action = wezterm.action_callback(function(window, pane)
              local url = window:get_selection_text_for_pane(pane)
              wezterm.log_info('opening: ' .. url)
              wezterm.open_with(url)
            end),
          },
        },

        -- 激活复制模式
        { key='v', mods='ALT|CTRL', action = wezterm.action.ActivateCopyMode },

        -- MacOS keybindings
        -- - 复制 黏贴
        { key="v", mods="CMD", action=wezterm.action{PasteFrom="Clipboard"},},
        -- { key="c", mods="CMD", action="Copy"},
        -- - 字体大小快捷键(放大/+、缩小/-、复原0) 
        { key="=", mods="CMD", action="IncreaseFontSize"},
        { key="-", mods="CMD", action="DecreaseFontSize"},
        { key="0", mods="CMD", action="ResetFontSize"},
        -- 隐藏
        { key="m", mods="CMD", action = "Hide"},

        -- 退出
        -- { key="q", mods="CMD",       action = "QuitApplication"},
        { key="q", mods="CMD|SHIFT", action="QuitApplication"},
        { key="q", mods="ALT|SHIFT", action="QuitApplication"},

    },

    -- 键表
    key_tables = {
        -- 复制模式
        copy_mode = {
          { key = 'c', mods = 'CTRL', action = act.CopyMode 'Close' },
          { key = 'g', mods = 'CTRL', action = act.CopyMode 'Close' },
          { key = 'q', mods = 'NONE', action = act.CopyMode 'Close' },
          { key = 'Escape', mods = 'NONE', action = act.CopyMode 'Close' },

          { key = 'h', mods = 'NONE', action = act.CopyMode 'MoveLeft' },
          { key = 'j', mods = 'NONE', action = act.CopyMode 'MoveDown' },
          { key = 'k', mods = 'NONE', action = act.CopyMode 'MoveUp' },
          { key = 'l', mods = 'NONE', action = act.CopyMode 'MoveRight' },

          { key = 'LeftArrow', mods = 'NONE', action = act.CopyMode 'MoveLeft' },
          { key = 'DownArrow', mods = 'NONE', action = act.CopyMode 'MoveDown' },
          { key = 'UpArrow', mods = 'NONE', action = act.CopyMode 'MoveUp' },
          {
            key = 'RightArrow',
            mods = 'NONE',
            action = act.CopyMode 'MoveRight',
          },

          {
            key = 'RightArrow',
            mods = 'ALT',
            action = act.CopyMode 'MoveForwardWord',
          },
          {
            key = 'f',
            mods = 'ALT',
            action = act.CopyMode 'MoveForwardWord',
          },
          {
            key = 'Tab',
            mods = 'NONE',
            action = act.CopyMode 'MoveForwardWord',
          },
          {
            key = 'w',
            mods = 'NONE',
            action = act.CopyMode 'MoveForwardWord',
          },

          {
            key = 'LeftArrow',
            mods = 'ALT',
            action = act.CopyMode 'MoveBackwardWord',
          },
          {
            key = 'b',
            mods = 'ALT',
            action = act.CopyMode 'MoveBackwardWord',
          },
          {
            key = 'Tab',
            mods = 'SHIFT',
            action = act.CopyMode 'MoveBackwardWord',
          },
          {
            key = 'b',
            mods = 'NONE',
            action = act.CopyMode 'MoveBackwardWord',
          },

          {
            key = '0',
            mods = 'NONE',
            action = act.CopyMode 'MoveToStartOfLine',
          },
          {
            key = 'Enter',
            mods = 'NONE',
            action = act.CopyMode 'MoveToStartOfNextLine',
          },

          {
            key = '$',
            mods = 'NONE',
            action = act.CopyMode 'MoveToEndOfLineContent',
          },
          {
            key = '$',
            mods = 'SHIFT',
            action = act.CopyMode 'MoveToEndOfLineContent',
          },
          {
            key = '^',
            mods = 'NONE',
            action = act.CopyMode 'MoveToStartOfLineContent',
          },
          {
            key = '^',
            mods = 'SHIFT',
            action = act.CopyMode 'MoveToStartOfLineContent',
          },
          {
            key = 'm',
            mods = 'ALT',
            action = act.CopyMode 'MoveToStartOfLineContent',
          },

          {
            key = ' ',
            mods = 'NONE',
            action = act.CopyMode { SetSelectionMode = 'Cell' },
          },
          {
            key = 'v',
            mods = 'NONE',
            action = act.CopyMode { SetSelectionMode = 'Cell' },
          },
          {
            key = 'V',
            mods = 'NONE',
            action = act.CopyMode { SetSelectionMode = 'Line' },
          },
          {
            key = 'V',
            mods = 'SHIFT',
            action = act.CopyMode { SetSelectionMode = 'Line' },
          },
          {
            key = 'v',
            mods = 'CTRL',
            action = act.CopyMode { SetSelectionMode = 'Block' },
          },

          {
            key = 'G',
            mods = 'NONE',
            action = act.CopyMode 'MoveToScrollbackBottom',
          },
          {
            key = 'G',
            mods = 'SHIFT',
            action = act.CopyMode 'MoveToScrollbackBottom',
          },
          {
            key = 'g',
            mods = 'NONE',
            action = act.CopyMode 'MoveToScrollbackTop',
          },

          {
            key = 'H',
            mods = 'NONE',
            action = act.CopyMode 'MoveToViewportTop',
          },
          {
            key = 'H',
            mods = 'SHIFT',
            action = act.CopyMode 'MoveToViewportTop',
          },
          {
            key = 'M',
            mods = 'NONE',
            action = act.CopyMode 'MoveToViewportMiddle',
          },
          {
            key = 'M',
            mods = 'SHIFT',
            action = act.CopyMode 'MoveToViewportMiddle',
          },
          {
            key = 'L',
            mods = 'NONE',
            action = act.CopyMode 'MoveToViewportBottom',
          },
          {
            key = 'L',
            mods = 'SHIFT',
            action = act.CopyMode 'MoveToViewportBottom',
          },

          {
            key = 'o',
            mods = 'NONE',
            action = act.CopyMode 'MoveToSelectionOtherEnd',
          },
          {
            key = 'O',
            mods = 'NONE',
            action = act.CopyMode 'MoveToSelectionOtherEndHoriz',
          },
          {
            key = 'O',
            mods = 'SHIFT',
            action = act.CopyMode 'MoveToSelectionOtherEndHoriz',
          },

          { key = 'PageUp', mods = 'NONE', action = act.CopyMode 'PageUp' },
          { key = 'PageDown', mods = 'NONE', action = act.CopyMode 'PageDown' },

          { key = 'b', mods = 'CTRL', action = act.CopyMode 'PageUp' },
          { key = 'f', mods = 'CTRL', action = act.CopyMode 'PageDown' },
        },
  },
}


-- 去掉标签标号（数字）
-- function Basename(s)
--     return string.gsub(s, "(.*[/\\])(.*)", "%2")
-- end

-- wezterm.on("format-tab-title", function(tab, tabs, panes, config, hover, max_width)
--     local pane = tab.active_pane
--     local title = Basename(pane.foreground_process_name)
--     return {{
--         Text = " " .. title .. " "
--     }}
-- end)

return config
